//
//  RMMAboutMineViewController.m
//  Romomo
//
//  Created by puman on 2017/2/17.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMAboutMineViewController.h"
#import <WebKit/WebKit.h>
@interface RMMAboutMineViewController ()<WKNavigationDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, assign) NSUInteger loadCount;
@property (strong, nonatomic) UIActivityIndicatorView *indicatorVIew;

@end

@implementation RMMAboutMineViewController
{
    NSString *_urlString;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:_progressView];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.progressView removeFromSuperview];
    [_indicatorVIew stopAnimating];
}
- (void)dealloc{
    [_wkWebView removeObserver:self forKeyPath:@"title" context:nil];
    [_wkWebView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = _titleName;
    self.view.backgroundColor = kCColor;
    _urlString = nil;
    if ([_titleName isEqualToString:@"关于我们"]) {
        _urlString = @"http://mp.weixin.qq.com/s/sJFZbZcbmIFE89VTUutz6A";
        [self addViews];
    } else if ([_titleName isEqualToString:@"订单退改规则"]){
        _urlString = @"http://vive.meijiewd.com/OrderChange.html";
        [self addViews];
        [self tuiGaiGuiZe];
    } else if ([_titleName isEqualToString:@"会员卡说明"]) {
        _urlString = @"http://vive.meijiewd.com/CardRule.html";
        [self addViews];
    } else if ([_titleName isEqualToString:@"商品购买须知"]){
        _urlString = @"http://vive.meijiewd.com/BuyNotice.html ";
        [self addViews];
    }
}
- (void)version{
    
}
- (void)addViews{
    self.view.backgroundColor = [UIColor whiteColor];
    _indicatorVIew = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(kScreenWideth / 2 - 50, 30, 100, 100)];
    _indicatorVIew.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [[UIApplication sharedApplication].keyWindow addSubview:_indicatorVIew];
    [_indicatorVIew startAnimating];
    _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    _wkWebView.navigationDelegate = self;
    [self loadRequest];
    [self.view addSubview:_wkWebView];
    [_wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [_wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    
    CGFloat progressBraHeight = 4.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height, navigationBarBounds.size.width, progressBraHeight);
    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:barFrame];
    progressView.tintColor = kBasicColor;
    progressView.trackTintColor = [UIColor whiteColor];
    _progressView = progressView;
    _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

}
-(void)loadRequest
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlString]];
    [_wkWebView loadRequest:request];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        if (object == _wkWebView) {
            [self.wkWebView setAlpha:1.0f];
            [_indicatorVIew stopAnimating];
            [self.progressView setProgress:_wkWebView.estimatedProgress animated:YES];
            if (_wkWebView.estimatedProgress >= 1.0f) {
                [UIView animateWithDuration:0.3 delay:0.3 options:(UIViewAnimationOptionCurveEaseOut) animations:^{
                    [self.progressView setAlpha:0.0f];
                } completion:^(BOOL finished) {
                    [self.progressView setProgress:0.0f animated:NO];
                }];
            }
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    } else if ([keyPath isEqualToString:@"title"]) {
        if (object == self.wkWebView) {
            self.title = self.wkWebView.title;
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)setLoadCount:(NSUInteger)loadCount {
    NSLog(@"%ld",loadCount);
    if (loadCount == 0) {
        self.progressView.hidden = YES;
        [self.progressView setProgress:0 animated:NO];
    }else {
        self.progressView.hidden = NO;
        CGFloat oldP = self.progressView.progress;
        CGFloat newP = (1.0 - oldP) / (loadCount + 1) + oldP;
        if (newP > 0.95) {
            newP = 0.95;
        }
        [self.progressView setProgress:newP animated:YES];
    }
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    if ([webView.URL.absoluteString containsString:@"itunes.apple.com"]) {
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
        decisionHandler(WKNavigationActionPolicyCancel);
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}
- (void)aboutMine{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, kScreenWideth, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"容么么欢迎您";
    [self.view addSubview:label];
}
- (void)tuiGaiGuiZe{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
